﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Yunwei.BizLogic;
using Yunwei.Domain;
using Yunwei.IBizLogic;
using Yunwei.Web.Areas.RRShop.Models;

namespace Yunwei.Web.Areas.RRShop.Controllers
{
    public class IngredientController : Controller
    {
        IRestaurantIngredientService _restaurantIngredientService;
        public IngredientController()
        {
            _restaurantIngredientService = new RestaurantIngredientService();
        }

        public ActionResult Index()
        {
            return View();
        }

        [Authorize]
        public ActionResult List(int id)
        {
            ViewBag.Id = id;

            var ris = _restaurantIngredientService.FindList(10, "", id, 1);
            List<RestaurantIngredientViewModel> rivms = new List<RestaurantIngredientViewModel>();
            foreach(var ri in ris)
            {
                rivms.Add(RestaurantIngredientViewModel.FromRestaurantIngredient(ri));
            }

            return View(rivms);
        }

        [Authorize]
        public ActionResult Add(int id)
        {
            ViewBag.Id = id;
            return View(new RestaurantIngredientViewModel() { Restaurant_Id = id });
        }

        [Authorize]
        [HttpPost]
        public ActionResult AddFromVendor(RestaurantIngredientViewModel rvm)
        {
            var ingredient =  _restaurantIngredientService.FindByIngredient(rvm.Ingredient_Id, rvm.Restaurant_Id);
            RespMessage<string> rm = new RespMessage<string>();

            if (ingredient != null)
            {
                //rvm.Stock += ingredient.Stock;
                //rvm.Stock = ingredient.Stock;
                //rvm.Id = ingredient.Id;
                ingredient.Ingredient_Id = rvm.Ingredient_Id;
                ingredient.Name = rvm.Name;
                ingredient.Restaurant_Id = rvm.Restaurant_Id;
                ingredient.Vendor_Id = rvm.Vendor_Id;
                ingredient.Cost = rvm.Cost;
                ingredient.Unit = rvm.Unit;

                rm.Success = _restaurantIngredientService.Update(ingredient);
            }
            else
            {
               var rim =  _restaurantIngredientService.Add(rvm.RestaurantIngredient);
               if (rim == null || rim.Id <= 0)
                   rm.Success = false;
               else
                   rm.Success = true;
            }

            return Json(rm);
        }


        [Authorize]
        public ActionResult Edit(int id,int rid)
        {
            ViewBag.Id = rid;
            var ri = _restaurantIngredientService.Find(id);
            if (ri == null)
                return RedirectToAction("Error404", "Home", new { Area = "Admin", returnUrl = "/RRShop/Ingredient/List/" + rid });

            return View(RestaurantIngredientViewModel.FromRestaurantIngredient(ri));
        }

        [Authorize]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(RestaurantIngredientViewModel rivm, int id, int rid)
        {
            ViewBag.Id = rid;
            var ri = _restaurantIngredientService.Find(id);
            if (ri == null)
                return RedirectToAction("Error404", "Home", new { Area = "Admin", returnUrl = "/RRShop/Ingredient/List/" + rid });
            else
            {
                ri.Stock = rivm.Stock;
                if (_restaurantIngredientService.Update(ri))
                {
                    ModelState.AddModelError("","修改成功");
                }
                else
                {
                    ModelState.AddModelError("", "修改失败");
                }
            }

            return View(rivm);
        }

        public ActionResult Delete(int id,int rid)
        {
            ViewBag.Id = rid;
            var ri = _restaurantIngredientService.Find(id);
            _restaurantIngredientService.Delete(ri);

            return RedirectToAction("List", new { Id = rid });
        }
    }
}